// pages/order-details/details.js
import {
  ajax,
  src,
  util
} from "../../utils/util.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    details:'',
  },
  // 订单详情
  getdetails: function () {
    ajax('/api/user/order_show', {
      data: {
        uqid: wx.getStorageSync("uqid"),
        order_id:this.data.id
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "get",
    }, res => {
      this.setData({
        details: res.data
      })
    })
  },
  // 取消订单
  cancelorder: function (e) {
    var id = e.currentTarget.dataset.id;
    ajax('/api/user/order_feat', {
      data: {
        uqid: wx.getStorageSync("uqid"),
        order_id: id,
        status: 1,
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "post",
    }, res => {
      if (res.error == '0') {
        wx.navigateTo({
          url: '/pages/my-order/order',
        })
      }
    })
  },
  // 确认收货
  shouhuo: function (e) {
    var id = e.currentTarget.dataset.id;
    ajax('/api/user/order_feat', {
      data: {
        uqid: wx.getStorageSync("uqid"),
        order_id: id,
        status: 4,
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "post",
    }, res => {
      if (res.error == '0') {
        this.getdetails()
      }
    })
  },
  // 去评论
  qupinglun: function (e) {
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/shop-comment/comment?id=' + id,
    })
  },
  // 申请售后
  after: function (e) {
    var orderid = e.currentTarget.dataset.orderid
    var shopid = e.currentTarget.dataset.shopid
    wx.navigateTo({
      url: '/pages/refund-after/after?orderid=' + orderid + '&shopid=' + shopid,
    })
  },
  // 支付
  zhifu: function (e) {
    var order_id = e.currentTarget.dataset.id
    ajax('/api/user/order_feat', {
      data: {
        uqid: wx.getStorageSync("uqid"),
        order_id: order_id,
        status: 2,
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "post",
    }, res => {
      console.log('去支付', res.data)
      if (res.error == '200') {
        wx.showToast({
          title: '支付成功',
          icon: 'success',
          duration: 3000
        })
      } else if (res.error == '0') {
        wx.requestPayment({
          timeStamp: res.data.timeStamp,
          nonceStr: res.data.nonceStr,
          package: res.data.package,
          signType: res.data.signType,
          paySign: res.data.paySign,
          success(res) {
            console.log(res)
            wx.redirectTo({
              url: '/pages/my-order/order',
            })
          },
          fail(res) {
            console.log(res)
          }
        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'success',
          duration: 3000
        })
      }

    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id:options.id
    })
    this.getdetails()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})